<?php

namespace App\depository;

use App\Models\RecordFriendKillModel;

class RecordFriendKillDep
{
    public $model;

    public function __construct()
    {
        $this->model = new RecordFriendKillModel();
    }

    public function add($data)
    {
        return $this->model->insert($data);
    }


    public function get_list_by_gh_id($gh_id){
        return $this->model->where('gh_id', $gh_id)->orderBy('id', 'desc')->limit(200)->get()->toArray();
    }
    public function list($param)
    {
        $res = $this->model
            ->when(!empty($param['id']), function ($query) use ($param) {
                return $query->where("id", $param['id']);
            })
            ->when(!empty($param['gh_id']), function ($query) use ($param) {
                return $query->where("gh_id", $param['gh_id']);
            })
            ->when(!empty($param['date'][0]), function ($query) use ($param) {
                return $query->where("date", ">=", $param['date'][0]);
            })
//            ->when(!empty($param['date'][1]),function($query) use($param){
//                return $query->where("date","<=",date("Y-m-d",strtotime("+1 day",strtotime($param['date'][1]))));
//            })
            ->when(!empty($param['date'][1]), function ($query) use ($param) {
                return $query->where("date", "<=", $param['date'][1]);
            })
            ->when(!empty($param['murderer_name']), function ($query) use ($param) {
                return $query->where("murderer_name", 'like', "%{$param['murderer_name']}%");
            })
            ->when(!empty($param['murderer_steamid']), function ($query) use ($param) {
                return $query->where("murderer_steamid", 'like', "%{$param['murderer_steamid']}%");
            })
            ->when(!empty($param['victim_name']), function ($query) use ($param) {
                return $query->where("victim_name", 'like', "%{$param['victim_name']}%");
            })
            ->when(!empty($param['victim_steamid']), function ($query) use ($param) {
                return $query->where("victim_steamid", 'like', "%{$param['victim_steamid']}%");
            })
            ->orderBy('id', 'desc')
            ->paginate($param['per_page'], ['*'], 'page', $param['current_page']);;
        return $res;
    }

    public function del($id)
    {
        return $this->model->whereIn('id', $id)->delete();
    }

    public function upd($id, $data)
    {
        return $this->model->where('id', $id)->update($data);
    }

    public function getRecent($teamId,$steamid)
    {
        $res = $this->model->where('gh_id',$teamId)
            ->where(function($query) use ($steamid){
                $query->where('murderer_steamid',$steamid);
                $query->orWhere('victim_steamid',$steamid);
            })->orderBy('id', 'desc')->limit(50)->get();
        return $res;
    }

}
